/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nominalitzacions.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author genis.sole
 */
public class SettingsView extends JFrame {
    private SettingsViewController SVC;

    private JPanel mainPanel = new JPanel();
    private JPanel variablesPanel = new JPanel();

    private JPanel varPanel = new JPanel();

    private JLabel KLabel = new JLabel("Suggestions number:");
    private JComboBox KCbox = new JComboBox();

    private JLabel NLabel = new JLabel("Maximum rule length:");
    private JComboBox NCbox = new JComboBox();

    private JPanel defVarPanel = new JPanel();
    private JLabel defVarLabel = new JLabel("Use default values");
    private JCheckBox defVarCheck= new JCheckBox();

    private JPanel fontPanel = new JPanel();

    private JPanel fPanel = new JPanel();
    private JPanel colorPanel = new JPanel();
    private JLabel colorLabel = new JLabel("Color:");
    private JTextArea colorTextField = new JTextArea("Sample text");
    private JButton colorButton = new JButton("Choose color");

    private JPanel defFontPanel = new JPanel();
    private JLabel defFontLabel = new JLabel("Use default color");
    private JCheckBox defFontCheck= new JCheckBox();

    private JPanel backPanel = new JPanel();
    private JPanel bPanel = new JPanel();

    private JPanel samplePanel = new JPanel();
    private JLabel backLabel = new JLabel("Color:");
    private JButton backButton = new JButton("Choose color");

    private JPanel defBackPanel = new JPanel();
    private JLabel defBackLabel = new JLabel("Use default background");
    private JCheckBox defBackCheck = new JCheckBox();

    private JPanel buttonsPanel = new JPanel();
    private JButton okButton = new JButton("OK");
    private JButton cancelButton = new JButton("Cancel");

    public SettingsView(SettingsViewController SVC) {
        this.SVC = SVC;
        iniComponents();
    }

     public void setVisible() {
        this.pack();
        this.setVisible(true);

    }

    private void iniComponents() {
        iniFrame();
        iniMainPanel();
        iniSettings();
        assingListeners();
    }

    private void iniFrame() {
        this.setMinimumSize(new Dimension(500, 450));
        this.setPreferredSize(this.getMinimumSize());
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        this.setTitle("Settings");
        JPanel contentPane = (JPanel) this.getContentPane();
        contentPane.add(mainPanel);
    }

    private void iniMainPanel() {
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
        iniVarPanel();
        iniFontPanel();
        iniBackPanel();
        iniButtonsPanel();
        mainPanel.add(variablesPanel);
        mainPanel.add(fontPanel);
        mainPanel.add(backPanel);
        mainPanel.add(buttonsPanel);
    }

    private void iniVarPanel() {
        //varPanel.setPreferredSize(new Dimension(230, 150));
        varPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        variablesPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Variables"),
                BorderFactory.createEmptyBorder(5,5,5,5)));

        GroupLayout layout = new GroupLayout(varPanel);
        varPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(KLabel)
            .addComponent(NLabel));
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(KCbox)
            .addComponent(NCbox));
        layout.setHorizontalGroup(hGroup);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(KLabel)
            .addComponent(KCbox));
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(NLabel)
            .addComponent(NCbox));
        layout.setVerticalGroup(vGroup);

        defVarPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        defVarPanel.add(defVarLabel);
        defVarPanel.add(defVarCheck);

        variablesPanel.setLayout(new BorderLayout());
        variablesPanel.add(varPanel, BorderLayout.NORTH);
        variablesPanel.add(defVarPanel, BorderLayout.CENTER);

        for (int i = 1; i <= 10; ++i) {
            KCbox.addItem(String.valueOf(i));
        }
        //KCbox.addItem("All");
        //String k = SVC.getK();
        //KCbox.setSelectedItem(k);
        for (int i = 1; i <= 5; ++i) {
            NCbox.addItem(String.valueOf(i));
        }
        //String n = SVC.getK();
        //KCbox.setSelectedItem(n);

    }

    private void iniFontPanel() {
        //colorTextField.s;
        colorPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        colorPanel.setOpaque(true);
        colorPanel.setBackground(Color.WHITE);
        colorTextField.setPreferredSize(new Dimension(60, 25));
        fPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        fontPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Highlight color"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
        GroupLayout layout = new GroupLayout(fPanel);
        fPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(colorLabel));
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(colorPanel));
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(colorButton));
        layout.setHorizontalGroup(hGroup);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(colorLabel)
            .addComponent(colorPanel)
            .addComponent(colorButton));
        layout.setVerticalGroup(vGroup);

        defFontPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        defFontPanel.add(defFontLabel);
        defFontPanel.add(defFontCheck);

        fontPanel.setLayout(new BorderLayout());
        fontPanel.add(fPanel, BorderLayout.NORTH);
        fontPanel.add(defFontPanel, BorderLayout.CENTER);

    }

     private void iniBackPanel() {
        bPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        samplePanel.setOpaque(true);
        samplePanel.setBackground(Color.WHITE);
        backPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createTitledBorder("Background color"),
                BorderFactory.createEmptyBorder(5,5,5,5)));
        samplePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        GroupLayout layout = new GroupLayout(bPanel);
        bPanel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(backLabel));
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(samplePanel));
        hGroup.addGroup(layout.createParallelGroup()
            .addComponent(backButton));
        layout.setHorizontalGroup(hGroup);

        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup(Alignment.BASELINE)
            .addComponent(backLabel)
            .addComponent(samplePanel)
            .addComponent(backButton));
        layout.setVerticalGroup(vGroup);

        defBackPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
        defBackPanel.add(defBackLabel);
        defBackPanel.add(defBackCheck);

        backPanel.setLayout(new BorderLayout());
        backPanel.add(bPanel, BorderLayout.NORTH);
        backPanel.add(defBackPanel, BorderLayout.CENTER);

    }

    private void iniButtonsPanel() {
        buttonsPanel.setLayout(new FlowLayout(FlowLayout.TRAILING));
        okButton.setPreferredSize(new Dimension(100, 25));
        cancelButton.setPreferredSize(new Dimension(100, 25));
        buttonsPanel.add(okButton);
        buttonsPanel.add(cancelButton);
    }

    private void assingListeners() {
        colorButton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedcolorButtons(event, "Verbs",
                        colorPanel.getBackground());
            }
        });

         backButton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedcolorButtons(event, "Background",
                        samplePanel.getBackground());
            }
        });

        okButton.addActionListener(new ActionListener() {
            public void actionPerformed (ActionEvent event) {
                actionPerformedOkButtons(event);
            }
        });

        /*colorTextField.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                try {
                    highlightTextArea(vc);
                } catch (BadLocationException ex) {
                    System.err.println(ex);
                }
            }

            public void removeUpdate(DocumentEvent e) {
                try {
                    highlightTextArea(vc);
                } catch (BadLocationException ex) {
                    System.err.println(ex);
                }
            }

            public void changedUpdate(DocumentEvent e) {
                try {
                    highlightTextArea(vc);
                } catch (BadLocationException ex) {
                    System.err.println(ex);
                }
            }

        });*/
    }

    private void actionPerformedcolorButtons(ActionEvent e, String msg, Color c) {
        Color newColor = JColorChooser.showDialog(this, "Choose " + msg
                + " Color", c);
        if (newColor != null) {
            if (e.getSource() == colorButton) {
                /*try {
                    highlightTextArea(newColor);
                    vc = newColor;
                } catch (BadLocationException ex) {
                    System.err.println(ex);
                }*/
                colorPanel.setBackground(newColor);
            } else {
                samplePanel.setBackground(newColor);
            }
        }
    }

    private void actionPerformedOkButtons(ActionEvent event) {
        boolean [] c = new boolean[3];
        c[0] = defVarCheck.isSelected();
        c[1] = defFontCheck.isSelected();
        c[2] = defBackCheck.isSelected();
        SVC.setChecks(c);
        String k = (String) KCbox.getSelectedItem();
        System.out.println("K al guardar" + k);
        SVC.setK(k);
        String n = (String) NCbox.getSelectedItem();
        System.out.println("N al guardar" + n);
        SVC.setN(n);
        SVC.setBg(samplePanel.getBackground());
        SVC.setHl(colorPanel.getBackground());
        try {
            SVC.saveSettings();
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    private void iniSettings() {
        boolean [] c = SVC.getChecks();
        defVarCheck.setSelected(c[0]);
        defFontCheck.setSelected(c[1]);
        defBackCheck.setSelected(c[2]);
        System.out.println("getK: " + SVC.getK());
        KCbox.setSelectedItem(SVC.getK());
        System.out.println("getN: " + SVC.getN());
        NCbox.setSelectedItem(SVC.getN());
        colorPanel.setBackground(SVC.getHl());
        samplePanel.setBackground(SVC.getBg());
    }
    /*
    private void highlightTextArea(Color c) throws BadLocationException {
        final Highlighter hilit;
        final Highlighter.HighlightPainter painter;
        hilit = new DefaultHighlighter();
        painter = new DefaultHighlighter.DefaultHighlightPainter(c);
        colorTextField.setHighlighter(hilit);
        hilit.addHighlight(0, colorTextField.getLineEndOffset(0), painter);
    }*/
}
